/*

   **********************************************************************
   *                                                                    *
   * This file contains the code for an image object for a content-     *
   * based image retrieval application that "summarizes" an image and   *
   * sends the resulting image "signature" to a server for              *
   * classification. The server returns the classification result in    *
   * the form of a Web page containing example images of the input      *
   * image's category.                                                  *
   *                                                                    *
   * The image class defined here is rather specific to this particular *
   * application and is not really designed for broad reuse (just yet). *
   *                                                                    *
   **********************************************************************
*/

/*
   **********************************************************************
   *                                                                    *
   * Gist   -- content-based image retrieval application for the gPhone *
   *                                                                    *
   * Copyright (C) 2011 Yong Zeng                                       *
   *                                                                    *
   * This file is part of Gist.                                       *
   *                                                                    *
   * Gist is free software; you can redistribute it and/or modify it  *
   * under the terms of the GNU General Public License as published by  *
   * the Free Software Foundation; either version 2 of the License, or  *
   * (at your option) any later version.                                *
   *                                                                    *
   * Gist is distributed in the hope that it will be useful, but      *
   * WITHOUT ANY WARRANTY; without even the implied warranty of         *
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
   * General Public License for more details.                           *
   *                                                                    *
   * You should have received a copy of the GNU General Public License  *
   * along with Goggle; if not, write to the Free Software Foundation,  *
   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.       *
   *                                                                    *
   **********************************************************************
*/

//----------------------- PACKAGE SPECIFICATION -------------------------

package vision.gistcomputing ;

//------------------------------ IMPORTS --------------------------------

// Android UI support
import android.widget.RadioGroup ;
import android.widget.EditText ;
import android.widget.Button ;
import android.view.View ;

// Android application and OS support
import android.app.Activity ;
import android.content.Context ;
import android.content.Intent ;
import android.content.SharedPreferences ;
import android.os.Bundle ;

// Java containers
import java.util.HashMap ;
import java.util.Map ;

import vision.gistcomputing.R;

//----------------------- THE SETTINGS ACTIVITY -------------------------

/**
   This class implements the settings screen of a content-based image
   retrieval application. This activity allows the user to tweak various
   parameters used within the application.
*/
public class SettingsActivity extends Activity {

/// The various Goggle settings are specified by a bunch of edit boxes
/// and other such UI elements.
///@{
private EditText   m_gist_estimator_editbox ;
private EditText   m_server_address_editbox ;
private RadioGroup m_upload_policy ;
///@}

// To keep the upload policy in the Settings object in sync with the
// radio buttons in the UI, we need to know which policy ID maps to which
// radio button ID and vice versa.
private final static
Map<Integer, Integer> m_radio_btn_map = new HashMap<Integer, Integer>() ;

private final static
Map<Integer, Integer> m_radio_btn_rmap = new HashMap<Integer, Integer>() ;

static
{
   m_radio_btn_map.put(Settings.SEND_ENTIRE_IMAGE, R.id.send_entire_image) ;
   m_radio_btn_map.put(Settings.SEND_GIST_VECTOR,  R.id.send_gist_vector) ;

   m_radio_btn_rmap.put(R.id.send_entire_image, Settings.SEND_ENTIRE_IMAGE) ;
   m_radio_btn_rmap.put(R.id.send_gist_vector,  Settings.SEND_GIST_VECTOR) ;
} ;

//-------------------------- INITIALIZATION -----------------------------

/// A convenience function for starting this activity.
public static final void start(Context C)
{
   C.startActivity(new Intent(C, SettingsActivity.class)) ;
}

/// When the settings activity is created, we set up the UI with the
/// current settings.
@Override protected void onCreate(Bundle saved_state)
{
   super.onCreate(saved_state) ;
   setContentView(R.layout.settings) ;

   SharedPreferences prefs = getSharedPreferences(Settings.NAME, 0) ;

   m_gist_estimator_editbox = (EditText) findViewById(R.id.ge_name_editbox) ;
   m_gist_estimator_editbox.setText(
      prefs.getString(Settings.GIST_ESTIMATOR, Settings.gist_estimator())) ;

   m_server_address_editbox = (EditText) findViewById(R.id.server_ip_editbox) ;
   m_server_address_editbox.setText(
      prefs.getString(Settings.SERVER_ADDRESS, Settings.server_address())) ;

   m_upload_policy = (RadioGroup) findViewById(R.id.upload_policy) ;
   m_upload_policy.check(m_radio_btn_map.get(
      prefs.getInt(Settings.UPLOAD_POLICY, Settings.upload_policy()))) ;

   Button B = (Button) findViewById(R.id.save_settings_btn) ;
   B.setOnClickListener(new View.OnClickListener() {
      public void onClick(View V) {
         save_settings() ;
      }}) ;
}

//---------------------- SAVING THE PREFERENCES -------------------------

// This method saves the values typed in this activity's UI to both the
// Goggle Settings object and to the Android shared preferences cache and
// then returns to the main activity.
private void save_settings()
{
   save_goggle_settings() ;
   save_android_preferences() ;
   finish() ;
}

// This method stuff the values typed in this activity's UI into the
// Goggle Settings object.
private void save_goggle_settings()
{
   Settings.gist_estimator(m_gist_estimator_editbox.getText().toString()) ;
   Settings.server_address(m_server_address_editbox.getText().toString()) ;
   Settings.upload_policy(
      m_radio_btn_rmap.get(m_upload_policy.getCheckedRadioButtonId())) ;
}

// This method retrieves the current settings from the Goggle Settings
// object and stores them in the Android's persistent shared preferences
// cache.
private void save_android_preferences()
{
   SharedPreferences prefs = getSharedPreferences(Settings.NAME, 0) ;
   SharedPreferences.Editor prefs_editor = prefs.edit() ;

   prefs_editor.putString(Settings.GIST_ESTIMATOR, Settings.gist_estimator()) ;
   prefs_editor.putString(Settings.SERVER_ADDRESS, Settings.server_address()) ;
   prefs_editor.putInt(Settings.UPLOAD_POLICY, Settings.upload_policy()) ;

   prefs_editor.commit() ;
}

//----------------------------- CLEAN-UP --------------------------------

/// When the settings activity is shutting down, we save the preferences
/// in Android's persistent cache (but not to the Goggle Settings object
/// because it is possible the user typed in some values but then
/// dismissed this activity with the back button/key, effectively
/// canceling the save operation).
@Override protected void onStop()
{
   save_android_preferences() ;
   super.onStop() ;
}

//-----------------------------------------------------------------------

} 
